package com.jyxd.web.controller.basic;

import com.jyxd.web.data.basic.BedBindRespirator;
import com.jyxd.web.service.assessment.basic.BedBindRespiratorService;
import com.jyxd.web.util.HttpCode;
import com.jyxd.web.util.UUIDUtil;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.Map;

@Controller
@RequestMapping(value = "/bedBindRespirator")
public class BedBindRespiratorController {

    private static Logger logger= LoggerFactory.getLogger(BedBindRespiratorController.class);

    @Autowired
    private BedBindRespiratorService bedBindRespiratorService;

    /**
     * 增加一条记录
     * @return
     */
    @RequestMapping(value = "/insert")
    @ResponseBody
    public String insert(@RequestBody BedBindRespirator bedBindRespirator){
        JSONObject json=new JSONObject();
        json.put("code", HttpCode.FAILURE_CODE.getCode());
        json.put("data",new ArrayList<>());
        json.put("msg","添加失败");
        bedBindRespirator.setId(UUIDUtil.getUUID());
        bedBindRespirator.setCreateTime(new Date());
        bedBindRespiratorService.insert(bedBindRespirator);
        json.put("code",HttpCode.OK_CODE.getCode());
        json.put("msg","添加成功");
        return json.toString();
    }

    /**
     * 更新记录
     * @param map
     * @return
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public String update(@RequestBody(required=false) Map<String,Object> map){
        JSONObject json=new JSONObject();
        json.put("code",HttpCode.FAILURE_CODE.getCode());
        json.put("msg","更新失败");
        BedBindRespirator bedBindRespirator=null;
        //清除其他的串口信息的床位列表
        /**
        List<BedBindRespirator> list=bedBindRespiratorService.selectByBedCode(map.get("bedCode").toString());
        if(list!=null && list.size()>0){
            for (BedBindRespirator bindRespirator : list) {
                bindRespirator.setBedCode(null);
                bedBindRespiratorService.update(bindRespirator);
            }
        }
         **/
        if(map!=null && map.containsKey("id")){
            if(map.get("id")!=null && !"".equals(map.get("id").toString())){
                bedBindRespirator=bedBindRespiratorService.queryData(map.get("id").toString());
            }else{
                bedBindRespirator=new BedBindRespirator();
                bedBindRespirator.setId(UUIDUtil.getUUID());
            }
            if(map.get("bedCode")!=null && !"".equals(map.get("bedCode").toString())){
                bedBindRespirator.setBedCode(map.get("bedCode").toString());
            }
            if(map.get("respiratorManufacturers")!=null && !"".equals(map.get("respiratorManufacturers").toString())){
                bedBindRespirator.setRespiratorManufacturers(map.get("respiratorManufacturers").toString());
            }
            if(map.get("respiratorNumber")!=null && !"".equals(map.get("respiratorNumber").toString())){
                bedBindRespirator.setRespiratorNumber(map.get("respiratorNumber").toString());
            }
            if(map.get("respiratorId")!=null && !"".equals(map.get("respiratorId").toString())){
                bedBindRespirator.setRespiratorId(map.get("respiratorId").toString());
            }
            if(map.get("comNumberId")!=null && !"".equals(map.get("comNumberId").toString())){
                bedBindRespirator.setComNumberId(map.get("comNumberId").toString());
            }
            if(map.get("comNumber")!=null && !"".equals(map.get("comNumber").toString())){
                bedBindRespirator.setComNumber(map.get("comNumber").toString());
            }
            if(map.get("comPuterNumber")!=null && !"".equals(map.get("comPuterNumber").toString())){
                bedBindRespirator.setComPuterNumber(map.get("comPuterNumber").toString());
            }
            bedBindRespirator.setStatus(1);
            bedBindRespirator.setCreateTime(new Date());
            if(map.get("id")!=null && !"".equals(map.get("id").toString())){
                bedBindRespiratorService.update(bedBindRespirator);
            }else{
                bedBindRespiratorService.insert(bedBindRespirator);
            }

            json.put("msg","更新成功");
        }
        json.put("code",HttpCode.OK_CODE.getCode());
        return json.toString();
    }
    /**
     * 删除异常生命体征表记录
     * @param map
     * @return
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public String delete(@RequestBody(required=false) Map<String,Object> map){
        JSONObject json=new JSONObject();
        json.put("code",HttpCode.FAILURE_CODE.getCode());
        json.put("msg","删除失败");
        if(map.containsKey("id")){
            BedBindRespirator bedBindRespirator=bedBindRespiratorService.queryData(map.get("id").toString());
            if(bedBindRespirator!=null){
                bedBindRespirator.setStatus(-1);
                bedBindRespiratorService.update(bedBindRespirator);
                json.put("msg","删除成功");
            }else{
                json.put("msg","删除失败，没有这个对象。");
                return json.toString();
            }
        }
        json.put("code",HttpCode.OK_CODE.getCode());
        return json.toString();
    }

    /**
     * 根据主键id查询异常生命体征表记录
     * @param map
     * @return
     */
    @RequestMapping(value = "/queryData",method= RequestMethod.POST)
    @ResponseBody
    public String queryData(@RequestBody(required=false) Map<String,Object> map){
        JSONObject json=new JSONObject();
        json.put("code",HttpCode.FAILURE_CODE.getCode());
        json.put("data",new ArrayList<>());
        json.put("msg","暂无数据");
        if(map !=null && map.containsKey("id")){
            BedBindRespirator bedBindRespirator=bedBindRespiratorService.queryData(map.get("id").toString());
            if(bedBindRespirator!=null){
                json.put("msg","查询成功");
                json.put("data",JSONObject.fromObject(bedBindRespirator));
            }
        }
        json.put("code",HttpCode.OK_CODE.getCode());
        return json.toString();
    }
}
